package Day2025_03_05;

import java.util.Stack;

public class LeetCode946 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int n1 = pushed.length, n2 = popped.length;
        if (n1 != n2) return false;
        Stack<Integer> ret = new Stack<>();
        int i = 0, j = 0;
        while (i < n1) {
            ret.push(pushed[i++]);
            while (!ret.isEmpty() && ret.peek() == popped[j]) {
                ret.pop();
                j++;
            }
        }
        return j == n2;
    }
}
